package win.smartown.android.scrollPager

import android.annotation.TargetApi
import android.content.Context
import android.os.Build
import android.util.AttributeSet
import android.widget.ScrollView

/**
 * Created by smartown on 2017/6/21 11:01.
 * Description:
 */
class ScrollableScrollView : ScrollView, ScrollState {

    constructor(context: Context) : super(context) {}

    constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {}

    constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {}

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int, defStyleRes: Int) : super(context, attrs, defStyleAttr, defStyleRes) {
    }

    override fun computeScroll() {
        super.computeScroll()
        onScroll()
    }

    override fun atTop(): Boolean {
        if (getChildAt(0) == null) {
            return true
        }
        return scrollY == 0
    }

    override fun atBottom(): Boolean {
        if (getChildAt(0) == null) {
            return true
        }
        return getChildAt(0).measuredHeight <= scrollY + height
    }

    override fun onScroll() {
        if (parent is OnPagerScrollListener) {
            (parent as OnPagerScrollListener).onPagerScroll(this, scrollY)
        }
    }

}
